package com.example.myapplication.Activity;


import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.FirstBook.MainActivity;
import com.example.myapplication.R;

import java.util.ArrayList;
import java.util.List;

public class Main5Activity extends AppCompatActivity implements ViewPager.OnPageChangeListener {


    private int i[] = {R.drawable.a2, R.drawable.a4, R.drawable.hhh, R.drawable.ren};
    private ViewPager vp_pager;
    private LinearLayout ll_container;
    List<ImageView> al;
    Context mContext;
    int prePosition;
    PollThread pThread;
    private ImageView m5Back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main5);
        initView();
        initData();
        pollint();

    }

    private void initData() {
        mContext = this;
        al = new ArrayList<ImageView>();
        for (int x = 0; x < i.length; x++) {
            ImageView iv = new ImageView(mContext);
            iv.setBackgroundResource(i[x]);
            al.add(iv);
            View v = new View(mContext);
            v.setBackgroundResource(R.drawable.point_normal);
            //有多少张图就放置几个点
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(15, 15);
            layoutParams.leftMargin = 30;
            ll_container.addView(v, layoutParams);
        }
        vp_pager.setAdapter(new Myadapter());
        vp_pager.setOnPageChangeListener(Main5Activity.this);
        vp_pager.setCurrentItem(al.size() * 1000); //这个是无线轮询的关键
        ll_container.getChildAt(0).setBackgroundResource(R.drawable.point_select);
        prePosition = 0;
    }

    private void initView() {
        vp_pager = (ViewPager) findViewById(R.id.vp_pager);
        ll_container = (LinearLayout) findViewById(R.id.ll_container);
        m5Back = (ImageView) findViewById(R.id.m5_back);
        m5Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            finish();
            }
        });
    }

    private void pollint() {
        pThread = new PollThread();
        pThread.start();
    }

    class PollThread extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        vp_pager.setCurrentItem(vp_pager.getCurrentItem() + 1);
                    }
                });
            }
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        int newPosition = position % al.size();
        ll_container.getChildAt(newPosition).setBackgroundResource(R.drawable.point_select);
        ll_container.getChildAt(prePosition).setBackgroundResource(R.drawable.point_normal);
        prePosition = newPosition;
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    class Myadapter extends PagerAdapter {

        @Override
        public int getCount() {
            return Integer.MAX_VALUE; // 要无限轮播
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            int position1 = position % al.size();
            ImageView imageView = al.get(position1);
            container.addView(imageView);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
